//
//  EFAddressMarkViewPicker.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/8.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFAddressMarkViewPicker.h"
#import "ICKeyValueBaseObj.h"

@interface EFAddressMarkViewPicker()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic, strong)UIView *pickerBgView;

@property (strong, nonatomic) UIPickerView *positionPicker;
@property (strong, nonatomic) NSMutableArray *positionArray;
@property (copy, nonatomic) NSString *selectPositionString;

@property (strong, nonatomic) EFRetrunReasonItem *selectReason;
@property(assign,nonatomic)Boolean isFromReason;

@end

@implementation EFAddressMarkViewPicker

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    if (self) {
        self.isFromReason = false;
        self.positionArray = [NSMutableArray array];
        [self allPosiiton];
        self.backgroundColor = [UIColor clearColor];
        UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
        bgView.backgroundColor = [UIColor blackColor];
        bgView.alpha = 0.6;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickCancelBtn)];
        [bgView addGestureRecognizer:tap];
        [self addSubview:bgView];
        [self initView];
    }
    return self;
}

-(instancetype)initWithArray:(NSMutableArray *)array{
    self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    if (self) {
        self.isFromReason = true;
        self.positionArray = [NSMutableArray array];
        [self.positionArray addObjectsFromArray:array];
        //self.selectReason = [[EFRetrunReasonItem alloc]init];
        self.selectReason = [self.positionArray objectAtIndex:0];
        self.backgroundColor = [UIColor clearColor];
        UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
        bgView.backgroundColor = [UIColor blackColor];
        bgView.alpha = 0.6;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickCancelBtn)];
        [bgView addGestureRecognizer:tap];
        [self addSubview:bgView];
        [self initView];
    }
    return self;
}

- (void)initView{
    self.pickerBgView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 180.f)];
    self.pickerBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.pickerBgView];
    
    [self.pickerBgView addSubview:self.positionPicker];
    UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44.f)];
    toolView.backgroundColor = [UIColor whiteColor];
    [self.pickerBgView addSubview:toolView];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(15, 10.f, 50, 25.f);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:kBlack333 forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(onClickCancelBtn) forControlEvents:UIControlEventTouchUpInside];
     cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [self.pickerBgView addSubview:cancelBtn];
    
    UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    okBtn.frame = CGRectMake(kScreenWidth - 65, 10.f, 50, 25.f);
    [okBtn setTitle:@"确定" forState:UIControlStateNormal];
    [okBtn setTitleColor:kBlack333 forState:UIControlStateNormal];
    [okBtn addTarget:self action:@selector(onClickOKBtn) forControlEvents:UIControlEventTouchUpInside];
 
    okBtn.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [self.pickerBgView addSubview:okBtn];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    line.backgroundColor = kBlackDDD;
    line.top = okBtn.bottom + 10;
    [self.pickerBgView addSubview:line];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)allPosiiton{
    
    NSString* item1 = @"无";
    NSString* item2 = @"家";
    NSString* item3 = @"公司";
    
    [self.positionArray addObject:item1];
    [self.positionArray addObject:item2];
    [self.positionArray addObject:item3];
    
    self.selectPositionString = item1;
     
}


- (void)onClickCancelBtn{
    [self hide];
}

- (void)onClickOKBtn{
    if(self.isFromReason){
        if (self.SuccessBlock2) {
            self.SuccessBlock2(self.selectReason);
        }
    }else{
        if (self.SuccessBlock) {
            self.SuccessBlock(self.selectPositionString);
        }
    }
    [self hide];
}

-  (void)show:(NSString*) selectMark{
    self.isShow = YES;
    if(selectMark != NULL && ![selectMark isEqualToString:@""]){
        NSInteger selectRow = [self.positionArray indexOfObject:selectMark] ;
        [self.positionPicker  selectRow:selectRow inComponent:0 animated:YES];     }
    [UIView animateWithDuration:0.3 animations:^{
        self.pickerBgView.bottom = kScreenHeight;
    }completion:nil];
}

-(void)showWithReason:(EFRetrunReasonItem *)reason{
    self.isShow = YES;
    if(reason != NULL){
        NSInteger selectRow = [self.positionArray indexOfObject:reason] ;
        [self.positionPicker  selectRow:selectRow inComponent:0 animated:YES];     }
    [UIView animateWithDuration:0.3 animations:^{
        self.pickerBgView.bottom = kScreenHeight;
    }completion:nil];
}

- (void)hide{
    [UIView animateWithDuration:0.3 animations:^{
        self.pickerBgView.top = kScreenHeight;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.isShow = NO;
    }];
}


#pragma mark - UI
- (UIPickerView *)positionPicker{
    if (!_positionPicker) {
        _positionPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 260)];
        _positionPicker.delegate = self;
        _positionPicker.showsSelectionIndicator = YES;
        
    }
    return _positionPicker;
}

#pragma mark - UIPickerDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.positionArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if(self.isFromReason){
        EFRetrunReasonItem* item = (EFRetrunReasonItem*)[self.positionArray objectAtIndex:row];
        return item.reason;
    }else{
        return [self.positionArray objectAtIndex:row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if(self.isFromReason){
        self.selectReason = (EFRetrunReasonItem*)[self.positionArray objectAtIndex:row];
    }else{
        self.selectPositionString = [self.positionArray objectAtIndex:row];
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = kBlackDDD;
        }
    }
    
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc] init];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setFont:[UIFont systemFontOfSize:18]];
        pickerLabel.backgroundColor = [UIColor clearColor];
        pickerLabel.textColor = [UIColor blackColor];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:0];
    [pickerLabel sizeToFit];
    
    return pickerLabel;
}

@end
